package order

import (
	"booking/common/handler"
	"booking/common/utils"
	"booking/common/validator"
	"booking/models"

	jsoniter "github.com/json-iterator/go"

	"github.com/gin-gonic/gin"
)

func Info(c *gin.Context) {
	var params struct {
		OrderNo string `json:"order_no" binding:"required"`
	}

	if err := c.ShouldBindJSON(&params); err != nil {
		handler.Error(validator.Translate(err), c)
		return
	}
	var order models.Order
	models.Db.Model(models.Order{}).
		Preload("Courier").
		Preload("Product").
		Where(map[string]any{
			"order_no": params.OrderNo,
		}).
		First(&order)
	if order.Id == 0 {
		handler.Error("订单不存在", c)
		return
	}

	var couriers []map[string]any = make([]map[string]any, 0)
	for _, value := range order.Courier {
		var attachments []models.Attachments = make([]models.Attachments, 0)
		_ = jsoniter.UnmarshalFromString(value.Attachments, &attachments)
		couriers = append(couriers, map[string]any{
			"recipient_name":    value.RecipientName,
			"recipient_phone":   value.RecipientPhone,
			"recipient_city":    value.RecipientCity,
			"recipient_address": value.RecipientAddress,
			"courier":           value.Courier,
			"tracking_no":       value.TrackingNo,
			"ship_time":         utils.FormatDate(value.ShipTime),
			"delivery_time":     utils.FormatDate(value.DeliveryTime),
			"attachments":       attachments,
		})
	}
	var products []map[string]any = make([]map[string]any, 0)
	for _, value := range order.Product {
		products = append(products, map[string]any{
			"product_desc":    value.ProductDesc,
			"quantity":        value.Quantity,
			"unit_price":      value.UnitPrice,
			"amount":          value.Amount,
			"received_amount": value.ReceivedAmount,
			"remark":          value.Remark,
		})
	}
	var attachments []models.Attachments = make([]models.Attachments, 0)
	_ = jsoniter.UnmarshalFromString(order.Attachments, &attachments)

	handler.Success(map[string]any{
		"order_no":          order.OrderNo,
		"source":            order.Source,
		"customer_name":     order.CustomerName,
		"signer_name":       order.SignerName,
		"sign_date":         utils.FormatDate(order.SignDate),
		"push_date":         utils.FormatDate(order.PushDate),
		"push_time":         order.PushTime,
		"project_recipient": order.ProjectRecipient,
		"consignee_city":    order.ConsigneeCity,
		"layout_person":     order.LayoutPerson,
		"registrant":        order.Registrant,
		"registration_no":   order.RegistrationNo,
		"settlement_method": order.SettlementMethod,
		"sign_company":      order.SignCompany,
		"status":            order.Status,
		"status_label":      models.OrderStatusMap[order.Status],

		"taxpayer_no":        order.TaxpayerNo,
		"legal_person_name":  order.LegalPersonName,
		"legal_person_phone": order.LegalPersonPhone,
		"legal_person_idno":  order.LegalPersonIdno,

		"recipient_name":    order.RecipientName,
		"recipient_phone":   order.RecipientPhone,
		"recipient_city":    order.RecipientCity,
		"recipient_address": order.RecipientAddress,
		"attachments":       attachments,
		"couriers":          couriers,
		"products":          products,
	}, c)

}
